#!/bin/bash

killPort() {
        ps -ef|grep eureka-service | grep -v grep | awk '{print $2}' | xargs kill -9
}

JAVA_OPTS=""
VERSION="1.0.0"
conffile="./conf/eureka-service.properties"
if [ -f "$conffile" ]; then
        while read -r line || [[ -n "$line" ]]; do
                cmd=${line:0:1}
                if [ ! "$cmd"  == "#" ] && [ ! "$cmd" == "" ]; then
			line=${line// /}
                        JAVA_OPTS="$JAVA_OPTS -D$line"
                fi
        done < $conffile
fi

for i in $*
        do echo $i
        arg2=`echo $i | grep  -i "jar_version"`
        if [ ! -z "$arg2" ]; then
                VERSION=${arg2:14}
        fi
        arg3=`echo $i | grep "\-D"`
        if [ ! -z "$arg3" ]; then
                JAVA_OPTS="$JAVA_OPTS $i"
        fi
done
echo "JAVA_OPTS $JAVA_OPTS"

killPort
jarfile="eureka-service-${VERSION}.jar"
export DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
rm -rf  "$DIR/$jarfile*"
wget -P $DIR/ http://10.200.1.206:8081/artifactory/PLATFORM-CORE/com/lvmama/vst/hhs/eureka-service/${VERSION}/$jarfile

echo "nohup java $JAVA_OPTS -Xms1g -Xmx1g -jar $DIR/$jarfile > "$DIR/hhs-service.output" 2>&1 &"
nohup java $JAVA_OPTS -Xms1g -Xmx1g -jar $DIR/$jarfile > "$DIR/eureka-service.output" 2>&1 &

